﻿@page "/admin/hardwareinfo"
@attribute [Authorize]
@attribute [RolePermission]
@inherits ComponentDefault
@using ThingsGateway.Admin.Application
@namespace ThingsGateway.Admin.Razor
<div class="row g-2 mx-1 form-inline">
    <div class="col-12 col-md-12">

        <Card IsShadow=true class="m-2 flex-fill" Color="Color.Primary">
            <HeaderTemplate>
                <div class="d-flex justify-content-between align-items-center w-100">
                    <span>@Localizer["SystemInfo"]</span>
                    <small class="text-muted">
                        @HardwareJob.HardwareInfo.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss")
                    </small>
                </div>
            </HeaderTemplate>

            <BodyTemplate>
                <EditorForm class="mt-3" AutoGenerateAllItem="false" IsDisplay Model="HardwareJob.HardwareInfo" ItemsPerRow=3 RowType=RowType.Inline LabelWidth=300>
                    <FieldItems>

                        <EditorItem @bind-Field="@context.OSName" GroupName="@Localizer["SystemInfo"]" />
                        <EditorItem @bind-Field="@context.OsArchitecture" GroupName="@Localizer["SystemInfo"]" />
                        <EditorItem @bind-Field="@context.OSVersion" GroupName="@Localizer["SystemInfo"]" />
                        <EditorItem @bind-Field="@context.Environment" GroupName="@Localizer["SystemInfo"]" />
                        <EditorItem @bind-Field="@context.FrameworkDescription" GroupName="@Localizer["SystemInfo"]" />
                        <EditorItem @bind-Field="@context.UUID" GroupName="@Localizer["SystemInfo"]" />
                        <EditorItem @bind-Field="@context.SystemRunTotalMinute" GroupName="@Localizer["SystemInfo"]" />
                        <EditorItem @bind-Field="@context.AppRunTotalMinute" GroupName="@Localizer["SystemInfo"]" />


                        <EditorItem @bind-Field="@context.Memory" GroupName="@Localizer["GCMemoryInfoConfig"]" GroupOrder=2 />
                        <EditorItem @bind-Field="@context.TotalAvailableMemory" GroupName="@Localizer["GCMemoryInfoConfig"]" GroupOrder=2 />
                        <EditorItem @bind-Field="@context.HighMemoryLoadThreshold" GroupName="@Localizer["GCMemoryConfig"]" GroupOrder=2 />

                        <EditorItem TValue="bool" TModel="HardwareInfo" @bind-Field="@context.IsServerGC" GroupName="@Localizer["GCMemoryConfig"]" GroupOrder=2>
                            <EditTemplate Context="value">
                                <div class="col-12">
                                    <h6></h6>
                                </div>
                            </EditTemplate>
                        </EditorItem>

                        <EditorItem @bind-Field="@context.IsServerGC" GroupName="@Localizer["GCMemoryInfoConfig"]" GroupOrder=2 />
                        <EditorItem @bind-Field="@context.GCLatencyMode" GroupName="@Localizer["GCMemoryInfoConfig"]" GroupOrder=2 />


                        <EditorItem @bind-Field="@context.WorkingSet" GroupName="@Localizer["GCMemoryInfo"]" GroupOrder=3 />
                        <EditorItem @bind-Field="@context.PrivateMemory" GroupName="@Localizer["GCMemoryInfo"]" GroupOrder=3 />
                        <EditorItem @bind-Field="@context.PeakWorkingSet" GroupName="@Localizer["GCMemoryInfo"]" GroupOrder=3 />
                        <EditorItem TValue="ulong" TModel="HardwareInfo" @bind-Field="@context.HeapSize" GroupName="@Localizer["GCMemoryInfo"]" GroupOrder=3>
                            <EditTemplate Context="value">
                                <div class="col-12">
                                    <h6></h6>
                                </div>
                            </EditTemplate>
                        </EditorItem>
                        <EditorItem @bind-Field="@context.HeapSize" GroupName="@Localizer["GCMemoryInfo"]" GroupOrder=3 />
                        <EditorItem @bind-Field="@context.TotalMemory" GroupName="@Localizer["GCMemoryInfo"]" GroupOrder=3 />
                        <EditorItem @bind-Field="@context.FragmentedBytes" GroupName="@Localizer["GCMemoryInfo"]" GroupOrder=3 />
                        <EditorItem @bind-Field="@context.CommittedBytes" GroupName="@Localizer["GCMemoryInfo"]" GroupOrder=3 />

                        <EditorItem TValue="ulong" TModel="HardwareInfo" @bind-Field="@context.AvailableMemory" GroupName="@Localizer["GCMemoryInfo"]" GroupOrder=3>
                            <EditTemplate Context="value">
                                <div class="col-12">
                                    <h6></h6>
                                </div>
                            </EditTemplate>
                        </EditorItem>

                        <EditorItem @bind-Field="@context.AvailableMemory" GroupName="@Localizer["GCMemoryInfo"]" GroupOrder=3 />
                        <EditorItem @bind-Field="@context.GCAvailableMemory" GroupName="@Localizer["GCMemoryInfo"]" GroupOrder=3 />
                        <EditorItem @bind-Field="@context.VirtualMemory" GroupName="@Localizer["GCMemoryInfo"]" GroupOrder=3 />


                        <EditorItem @bind-Field="@context.GcGen0Count" GroupName="@Localizer["GCAnalyzeInfo"]" GroupOrder=4 />
                        <EditorItem @bind-Field="@context.GcGen1Count" GroupName="@Localizer["GCAnalyzeInfo"]" GroupOrder=4 />
                        <EditorItem @bind-Field="@context.GcGen2Count" GroupName="@Localizer["GCAnalyzeInfo"]" GroupOrder=4 />
                        <EditorItem TValue="ulong" TModel="HardwareInfo" @bind-Field="@context.TotalAllocatedBytes" GroupName="@Localizer["GCAnalyzeInfo"]" GroupOrder=4>
                            <EditTemplate Context="value">
                                <div class="col-12">
                                    <h6></h6>
                                </div>
                            </EditTemplate>
                        </EditorItem>

                        <EditorItem @bind-Field="@context.TotalAllocatedBytes" GroupName="@Localizer["GCAnalyzeInfo"]" GroupOrder=4 />
                        <EditorItem @bind-Field="@context.TotalPauseDurationMs" GroupName="@Localizer["GCAnalyzeInfo"]" GroupOrder=4 />

                    </FieldItems>
                </EditorForm>



            </BodyTemplate>
        </Card>

    </div>
</div>

<div class="row g-2 mx-1 form-inline mt-2">

    <div class="col-12 col-md-12">
        <Card IsShadow=true class="m-2 flex-fill" Color="Color.Primary">
            <HeaderTemplate>
                @Localizer["HardwareInfo"]
            </HeaderTemplate>

            <BodyTemplate>
                <div class="d-flex align-items-center justify-content-center w-100 h-100">
                    <div class="row g-2 mx-1 form-inline d-flex justify-content-center w-100">
                        <!-- ✅ CPU 使用率 -->
                        <div class="col-12 col-md-4 justify-content-center h-100">
                            @{
                                var cpuUsage = HardwareJob.HardwareInfo.CpuRate;
                            }
                            <div class="d-flex flex-column align-items-center">
                                <Circle Width="200" class="m-3"
                                        Value=@((int)(cpuUsage*100<=100?cpuUsage*100:100))
                                        Color=@((cpuUsage*100>70?Color.Warning:Color.Success))
                                        StrokeWidth="4" ShowProgress=false>
                                    <div class="circle-hardware">
                                        <span>
                                            @Localizer["CpuUsage"] <i>@((cpuUsage * 100).ToString("F0") + " %")</i>
                                        </span>
                                    </div>
                                </Circle>
                                <div class="mt-1">
                                    <span>@(HardwareJob.HardwareInfo.Processor)</span>
                                </div>
                            </div>
                        </div>

                        <!-- ✅ 内存使用率 -->
                        <div class="col-12 col-md-4 justify-content-center h-100">
                            @{
                                var availableMemory = HardwareJob.HardwareInfo.AvailableMemory;
                                var memory = HardwareJob.HardwareInfo.Memory;
                            }
                            <div class="d-flex flex-column align-items-center">
                                <Circle Width="200" class="m-3"
                                        Value=@((int)(memory>availableMemory?100 - (availableMemory * 100.00 / memory):100))
                                        Color=@((availableMemory * 100.00 / memory<20?Color.Warning:Color.Success))
                                        StrokeWidth="4">
                                    <div class="circle-hardware">
                                        <h6>@((100 - (availableMemory * 100.00 / memory)).ToString("F2") + " %")</h6>
                                        <span>@Localizer["WorkingSet"] <i>@(HardwareJob.HardwareInfo.WorkingSet + " MB")</i></span>
                                        <span>@Localizer["AvailableMemory"] <i>@((availableMemory / 1024.00).ToString("F2") + " GB")</i></span>
                                        <span>@Localizer["TotalMemory"] <i>@((memory / 1024.00).ToString("F2") + " GB")</i></span>
                                    </div>
                                </Circle>
                                <div class="mt-1">
                                    <span>@Localizer["MemoryUsage"]</span>
                                </div>
                            </div>
                        </div>

                        <!-- ✅ 磁盘使用率 -->
                        <div class="col-12 col-md-4 justify-content-center h-100">
                            @{
                                var totalFreeSpace = HardwareJob.HardwareInfo.DriveInfo.TotalFreeSpace;
                                var totalSize = HardwareJob.HardwareInfo.DriveInfo.TotalSize;
                            }
                            <div class="d-flex flex-column align-items-center">
                                <Circle Width="200" class="m-3"
                                        Value=@((int)(totalSize>totalFreeSpace?100 - (totalFreeSpace * 100.00 / totalSize):100))
                                        Color=@((totalFreeSpace * 100.00 / totalSize<20?Color.Warning:Color.Success))
                                        StrokeWidth="4">
                                    <div class="circle-hardware">
                                        <h6>@((100 - (totalFreeSpace * 100.00 / totalSize)).ToString("F2") + " %")</h6>
                                        <span>@(HardwareJob.HardwareInfo.DriveInfo.VolumeLabel)</span>
                                        <span>@Localizer["AvailableDisk"] <i>@((totalFreeSpace / 1024.00 / 1024 / 1024).ToString("F2") + " GB")</i></span>
                                        <span>@Localizer["TotalDisk"] <i>@((totalSize / 1024.00 / 1024 / 1024).ToString("F2") + " GB")</i></span>
                                    </div>
                                </Circle>
                                <div class="mt-1">
                                    <span>@Localizer["DiskUsage"]</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </BodyTemplate>
        </Card>
    </div>
</div>


<div class="row g-2 mx-1 form-inline">

    <div class="col-12 col-md-12">

        <Card IsShadow=true class="m-2 flex-fill" Color="Color.Primary">
            <HeaderTemplate>
                @Localizer["HardwareInfoChart"]
            </HeaderTemplate>

            <BodyTemplate>
                <Chart @ref=LineChart OnInitAsync="OnInit" Height="var(--line-chart-height)" Width="100%" OnAfterInitAsync="() => { chartInit = true; return Task.CompletedTask; }" />
            </BodyTemplate>
        </Card>
    </div>
</div>
